home *** CD-ROM | disk | FTP | other *** search
/ Champak 145 / (Vol 145) Dec 21 2011.iso / Games / kill-the-dragons.swf / scripts / %3Cdefault package%3E / cable_interface.as next >
Encoding:
Text File  |  2011-12-21  |  7.3 KB  |  278 lines

  1. Interface = function()
  2. {
  3.    var sockets = this.sockets;
  4.    var obj = this;
  5.    var id = 0;
  6.    var cable;
  7.    for(var s in sockets)
  8.    {
  9.       var socket = sockets[s] = eval(sockets[s]);
  10.       for(var s in sockets)
  11.       {
  12.          if(!sockets[s].connected_by)
  13.          {
  14.             sockets[s].to = "";
  15.             sockets[s].cable_id = "";
  16.          }
  17.       }
  18.       socket.watch("connected_to",function(id, oldVal, newVal)
  19.       {
  20.          this.to = newVal._name;
  21.          return newVal;
  22.       }
  23.       );
  24.       socket.connected_by = null;
  25.       socket.connected_to = null;
  26.       socket.onEnterFrame = function()
  27.       {
  28.          if(this.connected_by)
  29.          {
  30.             cable = this.connected_by;
  31.             cable.onSwitchPlugs();
  32.             cable.cable_mc.onEnterFrame = cable.cable_mc.render;
  33.             cable.cable_mc.swapDepths(id);
  34.             if(cable.s0 == this)
  35.             {
  36.                cable.s0 = Mouse;
  37.             }
  38.             delete this.connected_by;
  39.             this = this.connected_to;
  40.             delete this.connected_to;
  41.          }
  42.          else
  43.          {
  44.             var colors = [16250195,12188824,10010364,16551502];
  45.             cable = new obj.Cable(obj,this,Mouse,colors[3],26);
  46.          }
  47.       };
  48.    }
  49. };
  50. var p = Interface.prototype = new MovieClip();
  51. p.zSort = function(cable)
  52. {
  53.    for(var c in this)
  54.    {
  55.       if(this[c] instanceof MovieClip)
  56.       {
  57.          var cable_mc = this[c];
  58.          cable_mc.swapDepths(cable.cable_mc);
  59.       }
  60.    }
  61. };
  62. p.Cable = function(main_clip, s0, s1, col, size, id)
  63. {
  64.    this.s0 = s0;
  65.    this.s1 = s1;
  66.    this.id = id;
  67.    var darken = (col >> 16) * 0.6 << 16 | (col >> 8 & 255) * 0.6 << 8 | (col & 255) * 0.6;
  68.    var nodesNum = 6;
  69.    var ten = 5;
  70.    var rel = 0.7;
  71.    var gr = 4;
  72.    var cable_mc = this.cable_mc = main_clip.createEmptyMovieClip("cable" + id,id);
  73.    if(s0 !== Mouse)
  74.    {
  75.       var sx = s0._x;
  76.       var sy = s0._y;
  77.    }
  78.    else
  79.    {
  80.       var sx = main_clip._parent.oppbossa1._x;
  81.       var sy = main_clip._parent.oppbossa1._y;
  82.    }
  83.    if(s1 !== Mouse)
  84.    {
  85.       var tx = s1._x;
  86.       var ty = s1._y;
  87.    }
  88.    else
  89.    {
  90.       var tx = main_clip._parent.oppbossa1._x;
  91.       var ty = main_clip._parent.oppbossa1._y;
  92.    }
  93.    var nodes = [];
  94.    var i = 0;
  95.    while(i < nodesNum)
  96.    {
  97.       nodes[i] = {x:sx + (tx - sx) / (nodesNum + 1) * (i + 1),y:sy + (ty - sy) / (nodesNum + 1) * (i + 1)};
  98.       i++;
  99.    }
  100.    var plug0 = cable_mc.attachMovie("plug","plug0",0,{_x:sx,_y:sy});
  101.    plug0.nextFrame();
  102.    var plug1 = cable_mc.attachMovie("plug","plug1",5);
  103.    var obj = this;
  104.    var d_mc;
  105.    var s_mc;
  106.    var o_mc;
  107.    cable_mc.render = function()
  108.    {
  109.       d_mc.removeMovieClip();
  110.       s_mc.removeMovieClip();
  111.       o_mc.removeMovieClip();
  112.       d_mc = this.createEmptyMovieClip("d_mc",2);
  113.       s_mc = this.createEmptyMovieClip("s_mc",3);
  114.       o_mc = this.createEmptyMovieClip("o_mc",4);
  115.       var s0 = obj.s0;
  116.       var s1 = obj.s1;
  117.       if(s0 !== Mouse)
  118.       {
  119.          var sx = s0._x;
  120.          var sy = s0._y;
  121.       }
  122.       else
  123.       {
  124.          var sx = main_clip._parent.oppbossa1._x;
  125.          var sy = main_clip._parent.oppbossa1._y;
  126.       }
  127.       if(s1 !== Mouse)
  128.       {
  129.          var tx = s1._x;
  130.          var ty = s1._y;
  131.       }
  132.       else
  133.       {
  134.          var tx = main_clip._parent.oppbossa1._x;
  135.          var ty = main_clip._parent.oppbossa1._y;
  136.       }
  137.       for(var a in nodes)
  138.       {
  139.          var node = nodes[a];
  140.          var a = Number(a);
  141.          var prevnode = a <= 0 ? {x:sx,y:sy} : nodes[a - 1];
  142.          var nextnode = a >= nodesNum - 1 ? {x:tx,y:ty} : nodes[a + 1];
  143.          var vx = node.vx;
  144.          var vy = node.vy;
  145.          var a_x = node.x;
  146.          var a_y = node.y;
  147.          var p_x = prevnode.x;
  148.          var p_y = prevnode.y;
  149.          var n_x = nextnode.x;
  150.          var n_y = nextnode.y;
  151.          vx += (p_x - a_x) / ten;
  152.          vy += (p_y - a_y) / ten;
  153.          vx += (n_x - a_x) / ten;
  154.          vy += (n_y - a_y) / ten;
  155.          vy += gr;
  156.          vx *= rel;
  157.          vy *= rel;
  158.          node.x = a_x + vx;
  159.          node.y = a_y + vy;
  160.          plug0._x = sx;
  161.          plug0._y = sy;
  162.          plug1._x = tx;
  163.          plug1._y = ty;
  164.          node.vx = vx;
  165.          node.vy = vy;
  166.          var nearVal = 0.005;
  167.          if(s0 !== Mouse && s1 !== Mouse && vx < nearVal && vx > - nearVal && vy < nearVal && vy > - nearVal)
  168.          {
  169.             delete this.onEnterFrame;
  170.          }
  171.       }
  172.       o_mc.lineStyle(size,darken,100);
  173.       d_mc.lineStyle(size * 1.2,0,100);
  174.       var val = 1.9;
  175.       for(var a in nodes)
  176.       {
  177.          var node = nodes[a];
  178.          var a = Number(a);
  179.          var prevnode = a <= 0 ? {x:sx,y:sy} : nodes[a - 1];
  180.          var a_x = node.x;
  181.          var a_y = node.y;
  182.          var p_x = prevnode.x;
  183.          var p_y = prevnode.y;
  184.          var last = a == nodesNum - 1;
  185.          if(last)
  186.          {
  187.             var plug_rel_x = (a_x + p_x) / 2 - plug1._x;
  188.             var plug_rel_y = (a_y + p_y) / 2 - plug1._y;
  189.          }
  190.          if((a_x + p_x) / 2 + val > a_x && (a_x + p_x) / 2 - val < a_x)
  191.          {
  192.             with(d_mc)
  193.             {
  194.                if(last)
  195.                {
  196.                   moveTo(tx,ty);
  197.                }
  198.                lineTo((a_x + p_x) / 2,(a_y + p_y) / 2);
  199.             }
  200.             with(s_mc)
  201.             {
  202.                if(last)
  203.                {
  204.                   moveTo(tx,ty);
  205.                }
  206.                lineTo((a_x + p_x) / 2,(a_y + p_y) / 2);
  207.             }
  208.             with(o_mc)
  209.             {
  210.                if(last)
  211.                {
  212.                   moveTo(tx,ty);
  213.                }
  214.                lineTo((a_x + p_x) / 2,(a_y + p_y) / 2);
  215.             }
  216.          }
  217.          else
  218.          {
  219.             with(d_mc)
  220.             {
  221.                if(last)
  222.                {
  223.                   moveTo(tx,ty);
  224.                }
  225.                curveTo(a_x,a_y,(a_x + p_x) / 2,(a_y + p_y) / 2);
  226.             }
  227.             with(s_mc)
  228.             {
  229.                if(last)
  230.                {
  231.                   moveTo(tx,ty);
  232.                }
  233.                curveTo(a_x,a_y,(a_x + p_x) / 2,(a_y + p_y) / 2);
  234.             }
  235.             with(o_mc)
  236.             {
  237.                if(last)
  238.                {
  239.                   moveTo(tx,ty);
  240.                }
  241.                curveTo(a_x,a_y,(a_x + p_x) / 2,(a_y + p_y) / 2);
  242.             }
  243.          }
  244.       }
  245.       plug1.mask._rotation = Math.atan2(plug_rel_y,plug_rel_x) * 180 / 3.14;
  246.       d_mc.curveTo((sx + a_x) / 2,(sy + a_y) / 2,sx,sy);
  247.       s_mc.curveTo((sx + a_x) / 2,(sy + a_y) / 2,sx,sy);
  248.       o_mc.curveTo((sx + a_x) / 2,(sy + a_y) / 2,sx,sy);
  249.    };
  250.    this.remove = function()
  251.    {
  252.       delete cable_mc.onEnterFrame;
  253.       d_mc.removeMovieClip();
  254.       s_mc.removeMovieClip();
  255.       o_mc.removeMovieClip();
  256.       plug0.removeMovieClip();
  257.       plug1.removeMovieClip();
  258.       cable_mc.removeMovieClip();
  259.    };
  260.    this.onConnecting = function()
  261.    {
  262.       plug1.nextFrame();
  263.       plug1.swapDepths(1);
  264.    };
  265.    this.onSwitchPlugs = function()
  266.    {
  267.       plug0.swapDepths(5);
  268.       plug0.prevFrame();
  269.       plug1.swapDepths(0);
  270.       var tp = plug0;
  271.       plug0 = plug1;
  272.       plug1 = tp;
  273.    };
  274.    cable_mc.onEnterFrame = cable_mc.render;
  275.    cable_mc.onEnterFrame();
  276. };
  277. Object.registerClass("cable_interface",Interface);
  278.